import java.util.*;

public class Test {
    // 题目1：二叉搜索树中的众数
    List<Integer> list = new LinkedList<>();
    int baseNum, maxCount, count;
    public int[] findMode(TreeNode root) {
        mode(root);
        int size = list.size();
        int[] ret = new int[size];
        for(int i = 0; i < size; i++){
            ret[i] = list.get(i);
        }
        return ret;
    }
    public void mode(TreeNode root){
        if(root == null){
            return;
        }
        mode(root.left);
        fun(root.val);
        mode(root.right);
    }

    public void fun(int val){
        if(val == baseNum){
            count++;
        }else{
            count = 1;
            baseNum = val;
        }
        if(count == maxCount){
            list.add(val);
        }
        if(count > maxCount){
            list.clear();
            list.add(val);
            maxCount = count;
        }
    }

    // 题目2：合并二叉树
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        TreeNode node = new TreeNode(root1.val + root2.val);
        node.left = mergeTrees(root1.left, root2.left);
        node.right = mergeTrees(root1.right, root2.right);
        return node;
    }

    // 题目3：二叉树中第二小的节点

}
